package com.yinzifan.util.config;

import java.net.MalformedURLException;
import java.net.URL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.googlecode.jsonrpc4j.spring.AutoJsonRpcClientProxyCreator;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImplExporter;

@Configuration
public class JSONRpcConfig {

	private static final Logger LOGGER = LoggerFactory.getLogger(JSONRpcConfig.class);

	@Bean
	public AutoJsonRpcServiceImplExporter rpcServiceImplExporter() {
		return new AutoJsonRpcServiceImplExporter();
	}
	
	@Bean
	@ConditionalOnProperty({"rpc.client.url", "rpc.client.base-package"})
	public AutoJsonRpcClientProxyCreator rpcClientProxyCreator(@Value("${rpc.client.url}") String url, @Value("${rpc.client.base-package}")String basePackage) {
		LOGGER.debug("url: {}", url);
		AutoJsonRpcClientProxyCreator creator = new AutoJsonRpcClientProxyCreator();
		try {
			creator.setBaseUrl(new URL(url));
		} catch (MalformedURLException e) {
			LOGGER.error("创建rpc服务地址错误 {}", e);
			e.printStackTrace();
		}
		creator.setScanPackage(basePackage);
		return creator;
	}
}
